package cn.com.haopy.yue.ai.db.service.hos.impl;


import cn.com.haopy.yue.ai.db.entity.arisk.AIRiskPriceStep;
import cn.com.haopy.yue.ai.db.mapper.arisk.AIRiskPriceStepMapper;
import cn.com.haopy.yue.ai.db.service.BaseServiceImpl;
import cn.com.haopy.yue.ai.db.service.hos.AIRiskPriceStepService;
import cn.com.haopy.yue.ai.db.utils.SqlInjectionExtUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Description: TODO
 * @Author DuTao
 * @Date 2023/9/25 14:49
 */
@Service
@Slf4j
public class AIRiskPriceStepServiceImpl extends BaseServiceImpl<AIRiskPriceStepMapper, AIRiskPriceStep> implements AIRiskPriceStepService {
    @Override
    public Page<AIRiskPriceStep> page(Integer page, Integer limit, String sort, String order) {
        if (SqlInjectionExtUtils.check(sort) || SqlInjectionExtUtils.check(order)) {
            throw new RuntimeException("存在非法字段");
        }

        QueryWrapper<AIRiskPriceStep> queryWrapper = new QueryWrapper<>();


        if (StringUtils.hasText(sort) && StringUtils.hasText(order)) {
            queryWrapper.orderBy(true, "asc".equals(order), sort);
        }
        return page(new Page<>(page, limit), queryWrapper);
    }

    public List<AIRiskPriceStep> listByPosition() {
        return list(Wrappers.<AIRiskPriceStep>lambdaQuery()
                .eq(AIRiskPriceStep::getEnabled, true)
                .orderByDesc(AIRiskPriceStep::getSortIndex)
        );
    }

}
